<?php
class Portofolio_Model extends CI_Model{
	
	
	 function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function getData($limit, $offset,$judul="",$jenis="0"){
		if($jenis == '0'){
			$sql="SELECT * from tbl_portofolio where judul like '%".$judul."%' order by id_portofolio desc limit $offset, $limit";
		}else{
			$sql="SELECT * from tbl_portofolio where judul like '%".$judul."%' and (jenis = '$jenis' or jenis = '0') order by id_portofolio desc limit $offset, $limit";
		}
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}

	function getData1($limit, $offset){
		$sql="SELECT * from tbl_portofolio where jenis = '1' or jenis = '0' order by id_portofolio desc limit $offset, $limit";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}

	function getData2($limit, $offset){
		$sql="SELECT * from tbl_portofolio where jenis = '2' or jenis = '0' order by id_portofolio desc limit $offset, $limit";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	
	function get_detail_data($id){
		$sql="SELECT * from tbl_portofolio where id_portofolio='".$id."'";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function getAllData(){
		$sql="SELECT * from tbl_portofolio order by id_portofolio desc";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}

	function count_all_num_rows($judul="",$jenis="0"){
		if($jenis == '0'){
			$sql="SELECT * from tbl_portofolio where judul like '%".$judul."%'";
		}else{
			$sql="SELECT * from tbl_portofolio where judul like '%".$judul."%' and (jenis = '$jenis' or jenis = '0')";
		}
		//echo $sql;
		$query=$this->db->query($sql);
		return $query->num_rows();
		
	}
	
	function insert_data($judul,$gambar,$slug,$konten,$alur,$fitur,$spek_hardware,$client,$demo,$tags,$jenis){
		
		$data=array('judul'=>$judul,
					'slug'=>$slug,
					'konten'=>$konten,
					'alur'=>$alur,
					'fitur'=>$fitur,
					'client'=>$client,
					'spek_hardware'=>$spek_hardware,
					'demo'=>$demo,
					'tgl_posting'=>date('Y-m-d'),
					'tags'=>$tags,
					'gambar'=>$gambar,
					'jenis'=>$jenis);
		$insert=$this->db->insert('tbl_portofolio',$data);
		return $insert;
	}
	
	function update_data($id,$judul,$gambar,$konten,$alur,$fitur,$spek_hardware,$client,$demo,$slug,$tags,$jenis){
		$data=array('judul'=>$judul,
					'konten'=>$konten,
					'alur'=>$alur,
					'fitur'=>$fitur,
					'slug'=>$slug,
					'client'=>$client,
					'spek_hardware'=>$spek_hardware,
					'demo'=>$demo,
					'tags'=>$tags,
					'gambar'=>$gambar,
					'jenis'=>$jenis);
		$this->db->where('id_portofolio',$id);
		$update=$this->db->update('tbl_portofolio',$data);
		return $update;
	}
	
	function delete_data($id){
		$data=array('id_portofolio'=>$id);
		$this->db->where($data);
		$delete=$this->db->delete('tbl_portofolio'); 
		return $delete;
	}
	
	function create_slug($title){
		$url_title=url_title($title);
		$sql="SELECT * from tbl_portofolio where slug='".$url_title."'";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			$acak=rand(0000,9999);
			return strtolower($url_title."-".$acak);
		}else{
			return strtolower($url_title);
		}
	}
	

}
?>